﻿#include "DADataManageWidget.h"
#include "ui_DADataManageWidget.h"
// qt
#include <QAction>
#include <QActionGroup>
#include <QIcon>
#include <QHeaderView>
#include <QDebug>
// api
#include "DAAppCore.h"
#include "DADataManagerInterface.h"
//===================================================
// using DA namespace -- 禁止在头文件using!!
//===================================================

using namespace DA;

//===================================================
// DADataManageWidget
//===================================================
DADataManageWidget::DADataManageWidget(QWidget* parent) : QWidget(parent), ui(new Ui::DADataManageWidget)
{
    ui->setupUi(this);
    _actionTableView = new QAction(this);
    _actionTableView->setObjectName("actionDataManageTableView");
    _actionTableView->setCheckable(true);
    _actionTableView->setIcon(QIcon(":/Icon/Icon/showDataInList.svg"));
    _actionTreeView = new QAction(this);
    _actionTreeView->setObjectName("actionDataManageTreeView");
    _actionTreeView->setCheckable(true);
    _actionTreeView->setIcon(QIcon(":/Icon/Icon/showDataInTree.svg"));
    _actionGroup = new QActionGroup(this);
    _actionGroup->addAction(_actionTableView);
    _actionGroup->addAction(_actionTreeView);
    _actionGroup->setExclusive(true);
    ui->toolButtonShowTableView->setDefaultAction(_actionTableView);
    ui->toolButtonShowTreeView->setDefaultAction(_actionTreeView);
    _actionTableView->setChecked(true);
    connect(_actionTableView, &QAction::triggered, this, &DADataManageWidget::onActionTableViewTriggered);
    connect(_actionTreeView, &QAction::triggered, this, &DADataManageWidget::onActionTreeViewTriggered);
    connect(ui->dataMgrTableView, &DADataManageTableView::doubleClicked, this, &DADataManageWidget::onDataMgrTableViewDoubleClicked);
    connect(ui->dataMgrTreeView, &DADataManageTreeView::doubleClicked, this, &DADataManageWidget::onDataMgrTreeViewDoubleClicked);
    retranslateUi();
}

DADataManageWidget::~DADataManageWidget()
{
    delete ui;
}

void DADataManageWidget::retranslateUi()
{
    _actionTableView->setToolTip(tr("show datas in table view"));
    _actionTreeView->setToolTip(tr("show datas in tree view"));
}

void DADataManageWidget::changeEvent(QEvent* e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        retranslateUi();
        break;

    default:
        break;
    }
}

void DADataManageWidget::onActionTableViewTriggered(bool on)
{
    if (on) {
        ui->stackedWidget->setCurrentWidget(ui->dataMgrTableView);
    }
}

void DADataManageWidget::onActionTreeViewTriggered(bool on)
{
    if (on) {
        ui->stackedWidget->setCurrentWidget(ui->dataMgrTreeView);
    }
}
/**
 * @brief 双击触发dataDbClicked
 * @param index
 */
void DADataManageWidget::onDataMgrTableViewDoubleClicked(const QModelIndex& index)
{
    DADataManagerInterface* dmgr = DAAppCore::getInstance().getDataManagerInterface();
    DAData d                     = dmgr->getData(index.row());
    qDebug() << "emit dataDbClicked(d)";
    emit dataDbClicked(d);
}

void DADataManageWidget::onDataMgrTreeViewDoubleClicked(const QModelIndex& index)
{
}
